﻿using Microsoft.SharePoint;
using DVK.SP2007.UniversalClient;
using System.Web;
using System.Collections.Generic;
using System;
using DVK.SP2007.Constants;
using System.Web.Caching;

namespace DVK.SP2007
{
    /// <summary>
    /// Helps access and manage information from Dvk Organisations list.
    /// </summary>
    public class OrganisationRepository
    {
        private SPWeb _web;
        /// <summary>
        /// Initializes a new instance of the <see cref="OrganisationsManager"/> class.
        /// </summary>
        /// <param name="web">The web.</param>
        public OrganisationRepository(SPWeb web)
        {
            _web = web;
        }

        /// <summary>
        /// Gets the DVK capable organisations ordered by name asc.
        /// Caches for 15 minutes using ASP.NET Cache.
        /// </summary>
        /// <returns></returns>
        public List<UnitInfo> GetDvkCapableOrganisations()
        {

            List<UnitInfo> organisations = new CacheProvider().GetCache()[Caching.DvkCapableOrganisationList] as List<UnitInfo>;
            if (organisations != null)
                return organisations;

            organisations = new List<UnitInfo>();

            var organisationItems = GetOrganizations();
            
            foreach (SPListItem item in organisationItems)
            {
                var isDvkCapable = (bool)item[Fields.DvkCapable.Name];
                if (!isDvkCapable)
                    continue;

                UnitInfo unitInfo = new UnitInfo()
                {
                    Code = item[Fields.OrganisationCode.Name] as string,
                    Name = item[Fields.OrganisationName.Name] as string
                };
                organisations.Add(unitInfo);
            }

            //Sort based on Name using lambda expression
            organisations.Sort((p1, p2) => p1.Name.CompareTo(p2.Name));
            new CacheProvider().GetCache().Add(Constants.Caching.DvkCapableOrganisationList, organisations, null, DateTime.Now.AddMinutes(15), TimeSpan.Zero, CacheItemPriority.Default, null);

            return organisations;
        }


        /// <summary>
        /// Gets items from the Organisations list.
        /// Caches for 15 minutes.
        /// </summary>
        /// <returns></returns>
        public SPListItemCollection GetOrganizations()
        {
            var organisations = new CacheProvider().GetCache()[Constants.Caching.DvkOrganisationListitems] as SPListItemCollection;

            if (organisations == null)
            {
                SPList organisationsList = _web.GetListFromUrl(Constants.Urls.ListOrganisations);
                organisations = organisationsList.Items;
                new CacheProvider().GetCache().Add(Constants.Caching.DvkOrganisationListitems, organisations, null, DateTime.Now.AddMinutes(15), TimeSpan.Zero, CacheItemPriority.Default, null);
            }

            return organisations;
        }
    } 
}